package com.zr.daoimpl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.zr.dao.OrderDao;
import com.zr.model.Cgoodstype;
import com.zr.model.Clogstate;
import com.zr.model.Corder;
import com.zr.model.Costate;
import com.zr.model.Cotype;
import com.zr.model.Cuser;

@Repository("showOrderDao")
public class OrderDaoImpl<T> extends BaseDaoImpl<T> implements OrderDao<T>{


	@Override
	public List<Corder> getAllOrderInfo(int page,int rows,int uid){
		String hql="from Corder where uid=:uid";
		Query que=  this.getCurrentSession().createQuery(hql);
	        que.setFirstResult((page-1)*rows);
	        que.setMaxResults(rows);
	        que.setInteger("uid", uid);
	        List<Corder> li=que.list();
	      
		  return li;
	}

	@Override
	public Long getCountByoid(int uid) {
		String hql="select count(oid) from Corder where uid=:uid";
		Query que=this.getCurrentSession().createQuery(hql);
		que.setInteger("uid",uid);
		  Long i=(Long)que.uniqueResult();
		return i;
	}

	@Override
	public int insertOrder(Corder corder,Cgoodstype goodstype,Clogstate logstate, Costate ostate,Cotype otype,int uid) {
		
		String hql="from Costate c where c.ostate =:state";
		Query que=this.getCurrentSession().createQuery(hql);
		que.setString("state", ostate.getOstate());
		Costate st=(Costate)que.uniqueResult();
		corder.setCostate(st);
		
		String hql1="from Cotype c where c.otype =:type";
		Query que1=this.getCurrentSession().createQuery(hql1);
		que1.setString("type",otype.getOtype() );
		Cotype oty=(Cotype) que1.uniqueResult();
		corder.setCotype(oty);
		
		String hql2="from Clogstate c where c.logstate =:logstate";
		Query que2=this.getCurrentSession().createQuery(hql2);
		que2.setString("logstate",logstate.getLogstate() );
		Clogstate ost=(Clogstate) que2.uniqueResult();
		corder.setClogstate(ost);
		
		String hql3="from Cgoodstype c where c.gtname =:gtname";
		Query que3=this.getCurrentSession().createQuery(hql3);
		que3.setString("gtname", goodstype.getGtname());
		Cgoodstype goodstp=(Cgoodstype) que3.uniqueResult();
		corder.setCgoodstype(goodstp);
		corder.setUid(uid);
		int i=0;
		i=(int)this.getCurrentSession().save(corder);
		if(i>0){
			i=1;
		}
		return i;
	}

	@Override
	public List<Corder> getCurrentOrderInfo(int page, int rows, int disid) {
		String hql="from Corder,Cprice,Cdisstation where Cdisstation.disid=:disid";
		Query que=this.getCurrentSession().createQuery(hql);
		que.setFirstResult((page-1)*rows);
		que.setMaxResults(rows);
		que.setInteger("disid",disid);
		List<Corder> li=que.list();
         return li;
	}

	@Override
	public int addOrder(Corder corder, Cgoodstype goodstype, Clogstate logstate, Costate ostate, Cotype otype) {
		// TODO Auto-generated method stub
		return 0;
	}





}
